import os
import random
import string
import time

import parsel

import sys
sys.path.append("..")

from src import facade
from src.facade import configdicts
from src.mylogger import MLogger


class DownList(object):
    def __init__(self):
        self.logger = MLogger().streamlogger
        self.listdir = configdicts["path"]["path_step1"]
        self.host = configdicts["other"]["host"]
        self.url = "http://" + self.host + "/Kns55/oldnavi/n_list.aspx?NaviID=100&Field=py_203&Value={}&NaviLink=拼音刊名%3a{}&DisplayMode=详细方式"
        facade.create_dir(self.listdir)
        self.count = 0
        # 检查是否需要重刷
        self.is_reload = False
        self.header = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Host": self.host,
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
        }

    def down_ascii(self):
        for ascii in string.ascii_uppercase:
            self.requests(ascii)
            time.sleep(2)

    def para(self, select, page):
        __EVENTTARGET = select.xpath('//*[@id="__EVENTTARGET"]/@value').get()
        if __EVENTTARGET is None:
            __EVENTTARGET = ""
        __EVENTARGUMENT = select.xpath('//*[@id="__EVENTARGUMENT"]/@value').get()
        if __EVENTARGUMENT is None:
            __EVENTARGUMENT = ""
        __VIEWSTATE = select.xpath('//*[@id="__VIEWSTATE"]/@value').get()
        if __VIEWSTATE is None:
            __VIEWSTATE = ""
        hidUID = select.xpath('//*[@id="hidUID"]/@value').get()
        if hidUID is None:
            hidUID = ""
        hidType = select.xpath('//*[@id="hidType"]/@value').get()
        if hidType is None:
            hidType = ""
        drpField = select.xpath('//*[@id="drpField"]/option[@selected="selected"]/@value').get()
        if drpField is None:
            drpField = ""
        txtValue = select.xpath('//*[@id="txtValue"]/@value').get()
        if txtValue is None:
            txtValue = ""
        DisplayModeRadio = "详细方式"
        drpAttach = select.xpath('//*[@id="drpAttach"]/option[@selected="selected"]/@value').get()
        if drpAttach is None:
            drpAttach = ""
        txtPageGoTo = select.xpath('//*[@id="txtPageGoTo"]/@value').get()
        if txtPageGoTo is None:
            txtPageGoTo = ""
        txtPageGoToBottom = select.xpath('//*[@id="txtPageGoToBottom"]/@value').get()
        if txtPageGoToBottom is None:
            txtPageGoToBottom = ""
        DisplayModeRadio1 = DisplayModeRadio
        """
        imgbtnGo.x = (1，40)
        imgbtnGo.y = (1,18)
        """
        return __EVENTTARGET, __EVENTARGUMENT, __VIEWSTATE, hidUID, hidType, drpField, txtValue, DisplayModeRadio, drpAttach, txtPageGoTo, txtPageGoToBottom, DisplayModeRadio1, random.randrange(
            1, 40), random.randrange(1, 18), page

    def requests(self, ascii):
        self.count = 0
        url = self.url.format(ascii, ascii)
        BoolResult, errString, r = facade.BaseRequest(url,
                                                      headers=self.header,
                                                      mark="lblCount",
                                                      timeout=(30, 60))
        if BoolResult:
            select = parsel.Selector(text=r.text)

            allpage = select.xpath('//*[@id="lblPageCount"]/text()').get()
            if int(allpage) == 0:
                return

            (__EVENTTARGET, __EVENTARGUMENT, __VIEWSTATE, hidUID, hidType, drpField,
             txtValue, DisplayModeRadio, drpAttach, txtPageGoTo, txtPageGoToBottom,
             DisplayModeRadio1, x, y, pageold) = self.para(select, 1)
            filepath = os.path.join(self.listdir, ascii + "_1" + ".html")
            if os.path.exists(filepath):
                self.logger.info("文件存在:{}".format(filepath))
                self.count = self.count + 1
            else:
                facade.single_write_file(r.text, filepath)
                self.count = self.count + 1
            for page in range(2, int(allpage) + 1):
                filepath = os.path.join(self.listdir, ascii + "_" + str(page) + ".html")
                if os.path.exists(filepath):
                    self.count = self.count + 1
                    self.logger.info("文件存在:{}".format(filepath))
                    continue
                if pageold + 1 == page:
                    # 代表是下一页 可以用下一页翻页请求,否者就要用跳转到某页请求
                    postdata = {
                        "__EVENTTARGET": "lbNextPage",
                        "__EVENTARGUMENT": __EVENTARGUMENT,
                        "__VIEWSTATE": __VIEWSTATE,
                        "hidUID": hidUID,
                        "hidType": hidType,
                        "drpField": drpField,
                        "txtValue": txtValue,
                        "DisplayModeRadio": DisplayModeRadio,
                        "drpAttach": drpAttach,
                        "txtPageGoTo": txtPageGoTo,
                        "DisplayModeRadio1": DisplayModeRadio1,
                        "txtPageGoToBottom": txtPageGoToBottom
                    }
                    BoolResult, errString, r = facade.BaseRequestPost(url,
                                                                      headers=self.header,
                                                                      data=postdata,
                                                                      mark="lblCount",
                                                                      timeout=(30, 60))
                    if BoolResult:
                        select = parsel.Selector(text=r.text)
                        (__EVENTTARGET, __EVENTARGUMENT, __VIEWSTATE, hidUID, hidType, drpField,
                         txtValue, DisplayModeRadio, drpAttach, txtPageGoTo, txtPageGoToBottom,
                         DisplayModeRadio1, x, y, pageold) = self.para(select, page)
                        filepath = os.path.join(self.listdir, ascii + "_" + str(page) + ".html")
                        self.logger.info(filepath)
                        facade.single_write_file(r.text, filepath)
                        self.count = self.count + 1
                    else:
                        self.logger.info("下载出错:" + errString)
                        facade.single_write_file(r.text, "./err_step1.html")
                else:
                    txtPageGoToBottom = str(page)
                    postdata = {
                        "__EVENTTARGET": __EVENTTARGET,
                        "__EVENTARGUMENT": __EVENTARGUMENT,
                        "__VIEWSTATE": __VIEWSTATE,
                        "hidUID": hidUID,
                        "hidType": hidType,
                        "drpField": drpField,
                        "txtValue": txtValue,
                        "DisplayModeRadio": DisplayModeRadio,
                        "drpAttach": drpAttach,
                        "txtPageGoTo": txtPageGoTo,
                        "DisplayModeRadio1": DisplayModeRadio1,
                        "txtPageGoToBottom": txtPageGoToBottom,
                        "imgbtnGo.x": str(x),
                        "imgbtnGo.y": str(y)
                    }
                    BoolResult, errString, r = facade.BaseRequestPost(url,
                                                                      headers=self.header,
                                                                      data=postdata,
                                                                      mark="lblCount",
                                                                      timeout=(30, 60))
                    if BoolResult:
                        select = parsel.Selector(text=r.text)
                        (__EVENTTARGET, __EVENTARGUMENT, __VIEWSTATE, hidUID, hidType, drpField,
                         txtValue, DisplayModeRadio, drpAttach, txtPageGoTo, txtPageGoToBottom,
                         DisplayModeRadio1, x, y, pageold) = self.para(select, page)
                        filepath = os.path.join(self.listdir, ascii + "_" + str(page) + ".html")
                        self.logger.info(filepath)
                        facade.single_write_file(r.text, filepath)
                        self.count = self.count + 1
                    else:
                        self.logger.info("下载出错:" + errString)
                        facade.single_write_file(r.text, "./err_step1.html")
                txtPageGoToBottom = str(page)

            if int(allpage) != self.count:
                print(int(allpage))
                print(self.count)
                print("**************************")
                print(ascii)
                self.is_reload = True
        else:
            self.logger.info("下载出错:" + errString)
            facade.single_write_file(r.text, "./err_step1.html")


if __name__ == "__main__":
    down = DownList()
    down.down_ascii()
    for i in range(1, 3):
        if down.is_reload:
            down.is_reload = False
            down.down_ascii()
